const router = require('koa-router')()
const {getType,recordBody,getBody} = require('../controller/body')
const {SuccessModel,ErrorModel} = require('../model/resModel.js')
router.prefix('/body')

router.get('/getType', async (ctx, next) => {
  const list = await getType()
  ctx.body=new SuccessModel(list)
})

router.post('/recordBody', async (ctx, next) => {
  let body=ctx.request.body
  body.userId=ctx.state['user'].id
  const val = await recordBody(body)
  if(val){
    ctx.body=new SuccessModel()
  }else{
    ctx.body=new ErrorModel('添加记录失败')
  }
})

router.get('/getBody', async (ctx, next) => {
  let query=ctx.query
  query.userId=ctx.state['user'].id
  const data=await getBody(query)
  ctx.body = new SuccessModel(data)
})

module.exports = router
